<?php

namespace app\api\controller;
use app\common\controller\Api;
use  think\db;

class Dates extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function getdata(){

        $thirtyDaysAgoTimestamp = strtotime('-30 days');

        $this->success('最低日期',date('Y-m-d',$thirtyDaysAgoTimestamp));

    }


    public function getalldatas()
    {
        $strtime=$this->request->param('strtime');
        $endtime=$this->request->param('endtime');

        if ($strtime=='' ){
            $strtime=date('Y-m-d',strtotime('-30 days'));
        }
        if ($endtime=='' ){
            $endtime=date('Y-m-d');
        }

        $date=$this->index($strtime,$endtime);

        $statusall0=0;
        $statusall1=0;
        $statusall2=0;
        $statusall3=0;


        foreach ($date as $vb){

            $list=Db::name('merch_lists')
                ->where('gettime','gt',$vb)
                ->where('user_id',$this->auth->id)
                ->where('gettime','lt',$vb+86400)
                ->select();

            $all=count($list);
            $status0=0;
            $status1=0;
            $status2=0;
            $status3=0;



            foreach ($list as $k=>$v){

                switch ($v['status']){
                    case 0:
                        $status0++;
                        $statusall0++;
                        break;
                    case 1:
                        $status1++;
                        $statusall1++;

                        break;
                    case 2:
                        $statusall2++;
                        $status2++;
                        break;
                    case 3:
                        $status3++;
                        $statusall3++;

                        break;
                }
            }

            $array[]=['date'=>date('m-d',$vb),'counts'=>['status0'=>$status0,'status1'=>$status1,'status2'=>$status2,'status3'=>$status3,'all'=>$all]];
        }


        $listall=$statusall0+$statusall1+$statusall2+$statusall3;

        $this->success('',['statusall0'=>$statusall0,'statusall1'=>$statusall1,'statusall2'=>$statusall2,'statusall3'=>$statusall3,'listall'=>$listall,'array'=>$array]);


    }
    public function index($str,$end){

        // 开始时间戳 和 结束时间戳
        $startTimestamp = strtotime($str); // 示例开始时间戳
        $endTimestamp   = strtotime($end); ; // 示例结束时间戳，比如：2024-05-01

        // 转换为 DateTime 对象
        $startDate = (new \DateTime())->setTimestamp($startTimestamp);
        $endDate   = (new \DateTime())->setTimestamp($endTimestamp);

        // 创建 DateInterval 对象（1天）
        $interval = new \DateInterval('P1D');

        // 创建 DatePeriod 对象
        $dateRange = new \DatePeriod($startDate, $interval, $endDate);

        // 遍历生成每一天
        $dates = [];
        foreach ($dateRange as $date) {
            $dates[] =strtotime( $date->format('Y-m-d'));
        }

        // 如果你想包含结束日期
        $dates[] =strtotime($endDate->format('Y-m-d'));


        $dates=array_reverse($dates);
        return $dates;
    }


}